package cn.tedu.straw.portal.service.impl;

import cn.tedu.straw.portal.dto.StudentRegisterDTO;
import cn.tedu.straw.portal.ex.ClassDisabledException;
import cn.tedu.straw.portal.ex.InsertException;
import cn.tedu.straw.portal.ex.InviteCodeException;
import cn.tedu.straw.portal.ex.PhoneDuplicateException;
import cn.tedu.straw.portal.mapper.ClassInfoMapper;
import cn.tedu.straw.portal.mapper.UserMapper;
import cn.tedu.straw.portal.mapper.UserRoleMapper;
import cn.tedu.straw.portal.model.ClassInfo;
import cn.tedu.straw.portal.model.User;
import cn.tedu.straw.portal.model.UserRole;
import cn.tedu.straw.portal.service.IUserService;
import cn.tedu.straw.portal.util.PasswordUtils;
import cn.tedu.straw.portal.vo.TeacherVO;
import cn.tedu.straw.portal.vo.UserLoginVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-10-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    ClassInfoMapper classInfoMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    @Transactional
    public void studentRegister(StudentRegisterDTO studentRegisterDTO){
        //1.检查密码是否正确
        //获取密码
        ClassInfo classInfo=classInfoMapper.findByInviteCode(studentRegisterDTO.getInviteCode());
        if(classInfo==null){
             throw new InviteCodeException("邀请码错误!");
        }
        //2.检查邀请码对应的班级是否启用
        //获取参数
        Integer isEnabled=classInfo.getIsEnabled();
        if(isEnabled==0){
            throw new ClassDisabledException("班级号被禁用!");
        }
        //检查手机号码是否被使用
        //获取参数
        String phone=studentRegisterDTO.getPhone();
        User result=userMapper.findByPhone(phone);
        if(result!=null){
            throw new PhoneDuplicateException("手机号被注册!");
        }
        //4.执行插入用户数据
        LocalDateTime now= LocalDateTime.now();
        User user=new User();
        user.setUsername(studentRegisterDTO.getUsername());
        user.setNickName(studentRegisterDTO.getNickname());
        //密码的加密处理
        String rawPassword=studentRegisterDTO.getPassword();
        String encodePassword= PasswordUtils.encode(rawPassword);
        user.setPassword(encodePassword);
        user.setGender(studentRegisterDTO.getGender());
        user.setDayOfBirth(studentRegisterDTO.getDayofBirth());
        user.setPhone(phone);
        user.setClassId(classInfo.getId());
        user.setIsEnabled(1);
        user.setIsLocked(0);
        user.setAccountType(0);
        user.setGmtCreate(now);
        user.setGmtModified(now);
        int rows=userMapper.insert(user);
        if(rows!=1){
          throw new InsertException("服务器忙!");
        }

        //向user_role表中插入数据,用于分配新注册的用户的角色
        UserRole userRole=new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(2);
        userRole.setGmtCreate(now);
        userRole.setGmtModified(now);
        rows=userRoleMapper.insert(userRole);
        if(rows!=1){
            throw new InsertException("注册失败!设置学生角色时出现错误,请稍后再试");
        }
    }


    @Override
    public UserLoginVO getLoginUserInfo(String phone){
        UserLoginVO userLoginVO=userMapper.findLoginVOByPhone(phone);
        return userLoginVO;
    }

    @Override
    public List<TeacherVO> getTeacherList() {
        return userMapper.findAllTeachers();
    }


}
